package com.gzwanchuang.xhgj.ui.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextWatcher;
import android.text.method.PasswordTransformationMethod;
import android.text.method.TransformationMethod;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.BindingAdapter;
import androidx.databinding.InverseBindingAdapter;
import androidx.databinding.InverseBindingListener;
import androidx.databinding.adapters.ListenerUtil;
import androidx.databinding.adapters.TextViewBindingAdapter;

import com.gzwanchuang.xhgj.R;
import com.gzwanchuang.xhgj.ui.utils.CountdownUtils;

/**
 * 文件名 : InputItemView
 * 创建者 : MangoWang
 * 创建日期 : 2019/12/17 15:42
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */
public class InputItemView extends FrameLayout {


    private Boolean canEdit;
    /**
     * 左边控件显示的文本
     */
    String textValue; // ?表示任何时候都不会报空指针异常
    /**
     * 输入控件提示显示的文本
     */
    String hintValue = ""; // ?表示任何时候都不会报空指针异常


    /**
     * 输入控件类型
     */
    String inputType = "";


    /**
     *
     */
    String itemTip = "";

    /**
     * 输入框的长度
     */
    int maxLine = 0;


    int leftImage = 0;

    /**
     * 事件是否是点击事件
     */
    boolean isShowMoreCode = false;
    private TextView tv_get_code;
    private TextView tv_tip;
    public EditText et_input;
    private CountdownUtils countdownUtils;


    public InputItemView(@NonNull Context context) {
        super(context);
    }

    public InputItemView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.InputItemView);
        textValue = typedArray.getString(R.styleable.InputItemView_itemName);
        hintValue = typedArray.getString(R.styleable.InputItemView_hintText);
        inputType = typedArray.getString(R.styleable.InputItemView_inputType);
        itemTip = typedArray.getString(R.styleable.InputItemView_itemTip);
        maxLine = typedArray.getInt(R.styleable.InputItemView_maxLines, maxLine);
        leftImage = typedArray.getResourceId(R.styleable.InputItemView_leftImage, R.mipmap.phone_bg);
        isShowMoreCode = typedArray.getBoolean(R.styleable.InputItemView_isShowMoreCode, isShowMoreCode);
        canEdit = typedArray.getBoolean(R.styleable.InputItemView_canEdit, true);
        initView();
        typedArray.recycle();
    }

    private void initView() {
        View.inflate(getContext(), R.layout.layout_input_item, this);

        tv_get_code = findViewById(R.id.tv_get_code);
        tv_tip = findViewById(R.id.tv_tip);
        et_input = findViewById(R.id.et_input);
        tv_get_code.setVisibility(isShowMoreCode ? View.VISIBLE : View.GONE);
        if (textValue != null) {
            et_input.setText(textValue);
        }

        et_input.setHint(hintValue);
        tv_tip.setText(itemTip);
        if (maxLine != 0) {
            et_input.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLine) {
            }});
        }
        switch (inputType) {
            case "password":
                et_input.setInputType(InputType.TYPE_CLASS_TEXT);
                et_input.setTransformationMethod(PasswordTransformationMethod.getInstance());
                break;
            case "number":
                et_input.setInputType(InputType.TYPE_CLASS_NUMBER);
                break;
            default:
                et_input.setInputType(InputType.TYPE_CLASS_TEXT);
        }

        findViewById(R.id.iv_left_image).setBackgroundResource(leftImage);
        et_input.setEnabled(canEdit);
        countdownUtils = new CountdownUtils(tv_get_code,null);
    }



    @BindingAdapter(value = "value", requireAll = false)
    public static void setValue(InputItemView customLayout, String value) {
        if (customLayout != null) {
            String edTextString = customLayout.et_input.getText() == null ? "" : customLayout.et_input.getText().toString();
            value = value == null ? "" : value;
            if (edTextString.equalsIgnoreCase(value)) {
                return;
            }
            customLayout.et_input.setText(value);
        }
    }

    @BindingAdapter("codeClick")
    public static void setCodeClick(InputItemView customLayout, OnClickListener value) {
        if (customLayout != null) {
            customLayout.tv_get_code.setOnClickListener(value);
        }
    }

    @BindingAdapter("startCode")
    public static void setStartCode(InputItemView customLayout, boolean value) {
        if (customLayout != null) {
            if (value) {
                customLayout.countdownUtils.RunTimer();
            } else {
                customLayout.countdownUtils.cancle();
            }
        }
    }

    @BindingAdapter("transformationMethod")
    public static void setTransformationMethod(InputItemView customLayout, TransformationMethod value) {
        if (customLayout != null) {
            customLayout.et_input.setTransformationMethod(value);
            customLayout.et_input.setSelection(customLayout.et_input.getText().length());
        }
    }

    @InverseBindingAdapter(attribute = "value", event = "valueAttrChanged")
    public static String getValue(InputItemView customLayout) {
        return customLayout.et_input.getText().toString();
    }

    @BindingAdapter(
            value = {"android:beforeTextChanged",
                    "android:onTextChanged",
                    "android:afterTextChanged",
                    "valueAttrChanged"},
            requireAll = false)
    public static void setTextWatcher(
            InputItemView view,
            final TextViewBindingAdapter.BeforeTextChanged before,
            final TextViewBindingAdapter.OnTextChanged on,
            final TextViewBindingAdapter.AfterTextChanged after,
            final InverseBindingListener valueAttrChanged) {
        TextWatcher newWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                if (before != null) {
                    before.beforeTextChanged(s, start, count, after);
                }
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (on != null) {
                    on.onTextChanged(s, start, before, count);
                }
                if (valueAttrChanged != null) {
                    valueAttrChanged.onChange();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (after != null) {
                    after.afterTextChanged(s);
                }
            }
        };
        TextWatcher oldWatcher = ListenerUtil.trackListener(view, newWatcher, R.id.textWatcher);
        if (oldWatcher != null) {
            view.et_input.removeTextChangedListener(oldWatcher);
        }
        view.et_input.addTextChangedListener(newWatcher);
    }
}
